﻿namespace SsisLdapSource
{
    using System;

    public partial class SsisLdapSource
    {
        private void ValidateCustomProperties()
        {
            DoesLdapConnectionContainerContainAllMandatoryProperties();
            DoesLdapQueryPropertiesContainerContainAllMandatoryFields();
        }

        private void DoesLdapConnectionContainerContainAllMandatoryProperties()
        {
            if (string.IsNullOrEmpty(HostOrDomainName))
            {
                throw new Exception("HostOrDomainName is a mandatory attribute");
            }
            
            if (LdapHostPortNumber == 0)
            {
                throw new Exception("PortNumber is a mandatory attribute");
            }

            
            if (!UseProcessSecurityContextFlag &&
                string.IsNullOrEmpty(ConnectionAccountName))
            {
                throw new Exception(
                    "You chose not to use task's security context, but did not provide ConnectionAccountName value");
            }

            if (!UseProcessSecurityContextFlag &&
                string.IsNullOrEmpty(ConnectionAccountPassword))
            {
                throw new Exception("You chose not to use task's security context, but did not provide ConnectionAccountPassword value");
            }
            
        }

        private void DoesLdapQueryPropertiesContainerContainAllMandatoryFields()
        {
            if (string.IsNullOrEmpty(SearchBaseDN))
            {
                throw new Exception("SearchBasedDN is a mandatory property");
            }

            if (string.IsNullOrEmpty(SearchFilter))
            {
                throw new Exception("SearchFilter is a mandatory property");
            }

            if (AttributesToLoad == null)
            {
                throw new Exception("AttributesToLoad is a mandatory property");
            }

            if (PageSize == 0)
            {
                throw new Exception("PageSize in a mandatory attribute");
            }

            if (string.IsNullOrEmpty(UniqueObjectIdentifierAttribute))
            {
                throw new Exception("UniqueObjectIdentifierAttribute is a mandatory property");
            }
        }
    }
}
